var FindAreaManage = new function() {
	
	var getAllCallback= function(data){
		
		if(data.type==='success'){
			$("#area1Id").val(data.data.area1Id);
			$("#area2Id").val(data.data.area2Id);
			$("#receiverArea").val(data.data.area3Id);
			FindAreaManage.getCity();
			FindAreaManage.getArea();
			FindAreaManage.getLandmark();
		}else{
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	}
	
	var getCityCallback= function(data){
		if(data.type==='success'){
			var cityId = $("#area1Id").val();
			var cityList = data.data;
			var citySelect = $("#area1Select");
			citySelect.html("<option value=''>选择省</option>");
			for(var i=0;i<cityList.length;i++){
				if(cityId == cityList[i].id){
					citySelect.append("<option value='"+cityList[i].id+"' selected='selected'>"+cityList[i].name+"</option>");
				}else{
					citySelect.append("<option value='"+cityList[i].id+"'>"+cityList[i].name+"</option>");
				}
			}
		}else{
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	}
	
	var getAreaCallback= function(data){
		if(data.type==='success'){
			var pcareaId = $("#area2Id").val();
			var areaList = data.data;
			var areaSelect = $("#area2Select");
			for(var i=0;i<areaList.length;i++){
				if(pcareaId == areaList[i].id){
					areaSelect.append("<option value='"+areaList[i].id+"' selected='selected'>"+areaList[i].name+"</option>");
				}else{
					areaSelect.append("<option value='"+areaList[i].id+"'>"+areaList[i].name+"</option>");
				}
			}
		}else{
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	}
	
	var getLandmarkCallback= function(data){
		if(data.type==='success'){
			var landmarkId = $("#receiverArea").val();
			var landmarkList = data.data;
			var landmarkSelect = $("#areaSelect");
			for(var i=0;i<landmarkList.length;i++){
				if(landmarkId == landmarkList[i].id){
					landmarkSelect.append("<option value='"+landmarkList[i].id+"' selected='selected'>"+landmarkList[i].name+"</option>");
				}else{
					landmarkSelect.append("<option value='"+landmarkList[i].id+"'>"+landmarkList[i].name+"</option>");
				}
			}
		}else{
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	}
	
	return {
		
		init : function() {
			this.setListener();
			this.getRegional();
		},
		
		setListener : function() {
			$("#area1Select").change(function(){
				$("#area1Id").val(this.value);
				FindAreaManage.resetArea();
				FindAreaManage.resetLandmark();
				FindAreaManage.getArea();
			});
			
			$("#area2Select").change(function(){
				$("#area2Id").val(this.value);
				FindAreaManage.resetLandmark();
				FindAreaManage.getLandmark();
			});
			
			$("#areaSelect").change(function(){
				$("#receiverArea").val(this.value);
			});
		},
		
		getRegional : function() {
			var landmarkId = $("#receiverArea").val();
			if(landmarkId != null && landmarkId != ''){
				FindAreaManage.getAll(landmarkId);
			}else{
				FindAreaManage.getCity();
			}
		},
		
		getAll : function(id) {
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"data" : {"id":id},
				"url" : ctx + "/sysArea/util/getAll",
				"success" : getAllCallback
			});
		},
		
		getCity : function() {
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"data" : {"id":"b0ef0ac827a611e69d5700000a00191c"},
				"url" :  ctx + "/sysArea/util/getByParentId",
				"success" : getCityCallback
			});
		},
		
		resetCity : function() {
			
		},
		
		getArea : function() {
			var id = $("#area1Id").val();
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"data" : {"id":id},
				"url" : ctx + "/sysArea/util/getByParentId",
				"success" : getAreaCallback
			});
		},
		
		resetArea : function() {
			$("#area2Id").val("");
			$("#area2Select").html("<option value=''>选择市</option>");
		},
		
		getLandmark : function() {
			var id = $("#area2Id").val();
			$.ajax({
				"dataType" : 'json',
				"type" : "POST",
				"data" : {"id":id},
				"url" : ctx + "/sysArea/util/getByParentId",
				"success" : getLandmarkCallback
			});
		},
		
		resetLandmark : function() {
			$("#receiverArea").val("");
			$("#areaSelect").html("<option value=''>选择区</option>");
		},
	}
	
}